package utiles_isi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class dateutil {
	//static String  DATE_FORMAT_="yyyy-MM-dd HH:mm:ss z";
	static String  DATE_FORMAT_="yyyy-MM-dd HH:mm:ss";
	
	public static double SecondsBetweenDates(Date a_,Date b_){
		return( Math.abs(a_.getTime()-b_.getTime())/1000); 
	}
	
	public static Date dateFromEpochString(String epochString){
		Long timestamp = Long.parseLong(epochString);
		Date date = new Date(timestamp * 1000);
		return date;
	}
	
	public static String now(String format_) {
	    if(format_.isEmpty()) 
	    	format_="yyyy-MM-dd HH:mm:ss";;
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    return sdf.format(cal.getTime());
}
	
	public static String fechahorastring(String format_,Date fecha_) {
	    if(format_.isEmpty()) 
	    format_="yyyy-MM-dd HH:mm:ss";
		//Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    return sdf.format(fecha_);
}
	
	public static String fechastring(String format_,Date fecha_) {
	    if(format_.isEmpty()) 
	    format_="yyy-MM-dd";
		//Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    return sdf.format(fecha_);
}
	public static String fechastring(String format_,String epochString_) {
	    if(format_.isEmpty())    format_="yyy-MM-dd";
		//Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    return sdf.format(dateFromEpochString(epochString_));
}
	public static Date dateFromString(String format_,String fecha_) {
	    if(format_.isEmpty())    format_="yyy-MM-dd";
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    Date dat_;
		try {
			dat_ = sdf.parse(fecha_);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	    return  dat_;
}
	
	public static Date AddDays(String format_,Date fecha_,int days_) {
		
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    cal.setTime(fecha_);
		cal.add(Calendar.DATE, days_);
		return cal.getTime();
}
	
	public static String AddDays(String format_,String fecha_,int days_) {
		Date dat_;
		if(format_.isEmpty())  	format_="yyy-MM-dd";
		   SimpleDateFormat sdf = new SimpleDateFormat(format_);
	    try {
	    	dat_=AddDays(format_,sdf.parse(fecha_),days_);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
	    return sdf.format(dat_);
}
	
	
	
}
